<?php

session_start();
include "../../db/dbconnect.php";
$query = "SELECT * FROM item";
$item_per_page = 6;

if(isset($_GET['page'])){
	$page = $_GET['page'];
}else{
	$page = "1";
}

if(isset($_GET['type'])){
	$type = $_GET['type'];
	$query .= " WHERE type=$type";
}

if(isset($_GET['name'])){
	$name = $_GET['name'];
	if(isset($_GET['type'])){
		$query .= " AND name LIKE '%{$name}%'";
	}else{
		$query .= " WHERE name LIKE '%{$name}%'";
	}
}

$start_from = $item_per_page*($page-1);
$result = mysql_query($query);
$cnt = mysql_num_rows($result);
$queryM = $query." LIMIT $start_from, $item_per_page";
$result = mysql_query($queryM);

$productsPerRow = 2;
$dbrow = mysql_num_rows($result);

$xml = new DOMDocument();
$xml->encoding = "UTF-8";
$xml->formatOutput = true;

$items = $xml->appendChild($xml->createElement('items'));
$total = $items->appendChild($xml->createElement('total'));
$total->appendChild($xml->createTextNode($cnt));

$itemInPage = $items->appendChild($xml->createElement('itemInPage'));
$itemInPage->appendChild($xml->createTextNode($dbrow));

if(isset($_SESSION['logged_in'])){
	$temp = 1;
}else{
	$temp = 0;
}

$login = $items->appendChild($xml->createElement('login'));
$login->appendChild($xml->createTextNode($temp));

if($dbrow > 0){
    while($row = mysql_fetch_array($result))
    {
    	$item = $items->appendChild($xml->createElement('item'));
    	$id = $item->appendChild($xml->createElement('id'));
    	$id->appendChild($xml->createTextNode($row['item_id']));
    	$name = $item->appendChild($xml->createElement('name'));
    	$name->appendChild($xml->createTextNode($row['name']));
    	$image = $item->appendChild($xml->createElement('image'));
    	$image->appendChild($xml->createTextNode('../../images/shop/'.$row['file']));
    	$price_buy = $item->appendChild($xml->createElement('price_buy'));
    	$price_buy->appendChild($xml->createTextNode($row['price_buy']));
    	$price_sell = $item->appendChild($xml->createElement('price_sell'));
    	$price_sell->appendChild($xml->createTextNode($row['price_sell']));
    	$effects = $item->appendChild($xml->createElement('effects'));
    	$effects->appendChild($xml->createTextNode($row['effects']));
    }
    $lastpage = $items->appendChild($xml->createElement('lastpage'));
    $lastpage->appendChild($xml->createTextNode(ceil($cnt/$item_per_page)));
}
header("Content-type: text/xml");
echo $xml->saveXML();

?>
